package com.weixin.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import com.weixin.po.MyX509TrustManager;

import net.sf.json.JSONObject;


public class Send {
	public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr){
		JSONObject jsonObject = null;  
		StringBuffer buffer = new StringBuffer();  
		try{
			TrustManager[] tm = { new MyX509TrustManager() };
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new java.security.SecureRandom()); 
			SSLSocketFactory ssf = sslContext.getSocketFactory();
			URL url = new URL(requestUrl); 
			HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
			httpUrlConn.setSSLSocketFactory(ssf);
			httpUrlConn.setDoOutput(true); 
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);
			httpUrlConn.setRequestMethod(requestMethod);  
			if ("GET".equalsIgnoreCase(requestMethod))  
				 httpUrlConn.connect();  
			 if (null != outputStr) { 
				 OutputStream outputStream = httpUrlConn.getOutputStream(); 
				 outputStream.write(outputStr.getBytes("UTF-8"));  
				 outputStream.close();  
			 }
			 InputStream inputStream = httpUrlConn.getInputStream();  
			 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
			 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
			 String str = null;  
			 while ((str = bufferedReader.readLine()) != null) {  
				 buffer.append(str);  
			 }
			 bufferedReader.close();  
			 inputStreamReader.close();   
			 inputStream.close();  
			 inputStream = null; 
			 httpUrlConn.disconnect();  
			 jsonObject = JSONObject.fromObject(buffer.toString()); 
			 
			 System.out.println(buffer.toString());
			 
		}catch (Exception e){
			e.printStackTrace();
		}
		return jsonObject; 
	}
}
